package com.hd.pzhwlxt.controller.model2;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hd.pzhwlxt.pojo.OfficeMaterial;
import com.hd.pzhwlxt.pojo.OfficeMaterialPurchase;
import com.hd.pzhwlxt.pojo.OfficeMaterialUsage;
import com.hd.pzhwlxt.service.OfficeMaterialPurchaseService;
import com.hd.pzhwlxt.service.OfficeMaterialService;
import com.hd.pzhwlxt.service.OfficeMaterialUsageService;
import com.hd.pzhwlxt.utils.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @create: 2022-09-23 11:25
 * @author: 怀念天空
 * @Description: 办公耗材信息模块的增删改查
 */
@RestController
@RequestMapping("/officeMaterial")
public class OfficeMaterialController {

    @Autowired
    private OfficeMaterialService officeMaterialService;

    @Autowired
    private OfficeMaterialPurchaseService officeMaterialPurchaseService;

    @Autowired
    private OfficeMaterialUsageService officeMaterialUsageService;

    @GetMapping("/get")
    public Response<Object> getAllOfficeMaterial(@RequestParam(value = "pageNum",required = false) Integer pageNum, @RequestParam(value = "pageSize",required = false) Integer pageSize) {
        if (pageNum == null || pageSize == null){
            List<OfficeMaterial> result = officeMaterialService.list();
            if(result!=null){
                return Response.createSuccessResponse("查询成功", result);
            }
        }else {
            Page<OfficeMaterial> result = officeMaterialService.page(new Page<>(pageNum, pageSize), null);
            if(result!=null){
                return Response.createSuccessResponse("查询成功", result);
            }
        }
        return Response.createErrorResponse("查询失败");
    }

    @GetMapping("/get/{id}")
    public Response<OfficeMaterial> getOfficeMaterialById(@PathVariable Integer id){
        OfficeMaterial material = officeMaterialService.getById(id);
        if(material!=null){
            return Response.createSuccessResponse("查询成功", material);
        }
        return Response.createErrorResponse("查询失败");
    }

    /**
     * 添加材料信息，并非采购记录,添加之前先查询是否存在，否则修改数量
     * @param officeMaterial
     * @return
     */
    @PostMapping("/add")
    public Response<String> addOfficeMaterial(@RequestBody OfficeMaterial officeMaterial){
        boolean save = officeMaterialService.save(officeMaterial);
        if (save){
            return Response.createSuccessResponse("添加成功");
        }
        return Response.createErrorResponse("添加失败");
    }

    @DeleteMapping("/del/{ids}")
    public Response<String> delOfficeMaterial(@PathVariable Integer[] ids){
        boolean remove = officeMaterialService.removeBatchByIds(Arrays.asList(ids));
        if (remove){
            return Response.createSuccessResponse("删除成功");
        }
        return Response.createErrorResponse("删除失败");
    }

    @PutMapping("/update")
    public Response<String> updateOfficeMaterial(@RequestBody OfficeMaterial officeMaterial){
        boolean update = officeMaterialService.updateById(officeMaterial);
        if (update){
            return Response.createSuccessResponse("修改成功");
        }
        return Response.createErrorResponse("修改失败");
    }

    /**
     * 采购模块，可以添加采购和查看采购记录
     * 注意：添加采购后，需手动确认采购状态，和支付状态。一旦采购成功不可修改，因为库存已经改变。
     */
    @GetMapping("/purchaseInfo")
    public Response<Object> getAllPurchaseInfo(@RequestParam(value = "pageNum",required = false) Integer pageNum, @RequestParam(value = "pageSize",required = false) Integer pageSize){
        if (pageNum == null || pageSize == null){
            List<OfficeMaterialPurchase> result = officeMaterialPurchaseService.list();
            if(result!=null){
                return Response.createSuccessResponse("查询成功", result);
            }
        }else {
            Page<OfficeMaterialPurchase> result = officeMaterialPurchaseService.page(new Page<>(pageNum, pageSize), null);
            if(result!=null){
                return Response.createSuccessResponse("查询成功", result);
            }
        }
        return Response.createErrorResponse("查询失败");
    }

    /**
     * 添加采购时是默认采购中，需手动确定收货
     * @param officeMaterialPurchase
     * @return
     */
    @PostMapping("/purchaseInfo")
    public Response<String> addPurchaseInfo(@RequestBody OfficeMaterialPurchase officeMaterialPurchase){
        //添加时先计算金额
        boolean save = officeMaterialPurchaseService.addPurchaseInfo(officeMaterialPurchase);
        if (save){
            return Response.createSuccessResponse("添加成功");
        }
        return Response.createErrorResponse("添加失败");
    }

    @PutMapping("/purchaseInfo")
    public Response<String> updatePurchaseInfo(@RequestBody OfficeMaterialPurchase officeMaterialPurchase){
        //修改时添加库存信息
        boolean update = officeMaterialPurchaseService.updatePurchaseInfo(officeMaterialPurchase);
        if (update){
            return Response.createSuccessResponse("添加成功");
        }
        return Response.createErrorResponse("添加失败");
    }

    /**
     * 耗材使用记录，添加使用记录和查询使用记录，添加会更新库存（提交了数据就不能修改，因为库存已经变化）
     */
    @GetMapping("/officeMaterialUsage")
    public Response<Object> officeMaterialUsageInfo(@RequestParam(value = "pageNum",required = false) Integer pageNum, @RequestParam(value = "pageSize",required = false) Integer pageSize){
        if (pageNum == null || pageSize == null){
            List<OfficeMaterialUsage> result = officeMaterialUsageService.list();
            if(result!=null){
                return Response.createSuccessResponse("查询成功", result);
            }
        }else {
            Page<OfficeMaterialUsage> result = officeMaterialUsageService.page(new Page<>(pageNum, pageSize), null);
            if(result!=null){
                return Response.createSuccessResponse("查询成功", result);
            }
        }
        return Response.createErrorResponse("查询失败");
    }
    /**
     * 添加耗材记录，根据id查询材料，更新库存
     */
    @PostMapping("/officeMaterialUsage")
    public Response<String> addOfficeMaterialUsage(@RequestBody OfficeMaterialUsage officeMaterialUsage){
        //添加时先计算金额
        boolean save = officeMaterialUsageService.addOfficeMaterialUsage(officeMaterialUsage);
        if (save){
            return Response.createSuccessResponse("添加成功");
        }
        return Response.createErrorResponse("添加失败");
    }

}
